const app = getApp()
const mockData = require('../../utils/data.js')

Page({
  data: {
    order: null,
    rating: 5,
    content: '',
    images: [],
    anonymous: false
  },
  
  onLoad(options) {
    const orderId = parseInt(options.orderId)
    const orders = app.globalData.orders || []
    const order = orders.find(o => o.id === orderId) || {
      id: orderId,
      products: [mockData.products[0]]
    }
    
    this.setData({ order })
  },
  
  selectRating(e) {
    const rating = e.currentTarget.dataset.rating
    this.setData({ rating })
  },
  
  onContentInput(e) {
    this.setData({
      content: e.detail.value
    })
  },
  
  toggleAnonymous() {
    this.setData({
      anonymous: !this.data.anonymous
    })
  },
  
  chooseImage() {
    wx.chooseImage({
      count: 9,
      success: (res) => {
        const images = [...this.data.images, ...res.tempFilePaths].slice(0, 9)
        this.setData({ images })
      }
    })
  },
  
  removeImage(e) {
    const index = e.currentTarget.dataset.index
    const images = this.data.images
    images.splice(index, 1)
    this.setData({ images })
  },
  
  publish() {
    if (!this.data.content.trim()) {
      wx.showToast({
        title: '请输入评价内容',
        icon: 'none'
      })
      return
    }
    
    // 更新订单状态
    const orders = app.globalData.orders || []
    const index = orders.findIndex(o => o.id === this.data.order.id)
    if (index !== -1) {
      orders[index].status = 'completed'
      app.globalData.orders = orders
      wx.setStorageSync('orders', orders)
    }
    
    wx.showToast({
      title: '评价成功',
      icon: 'success'
    })
    
    setTimeout(() => {
      wx.navigateBack()
    }, 1500)
  },
  
  cancel() {
    wx.navigateBack()
  }
})

